#ifndef _BOARD_H_
#define _BOARD_H_

#include "fm33le0xx_fl.h"

#define PIN_HIGH 1
#define PIN_LOW 0

#define DEVICE_NB_METER		"01"
#define DEVICE_485_METER	"11"

/*软件版本*/
#define SOFTWARE_VERSION "1.0.0.0"
/*设备类型*/
#define DEVICE_TYPE	"01"
/*硬件版本*/
#define METER_485_V30 		1
#define METER_NB_V20		2
#define LORA_METER_FM_V1_0	3
#define NB_SHOCKER_FM_V1_0	4


#define BOARD_VERSION		METER_485_V30

#define ADC_VREF    	(*((uint16_t *)(0x1FFFFB0C)))   //VREF1P0电压值（*10000）
#define ADC_TS       	(*((uint16_t *)(0x1FFFFa90)))   //PTAT电压值（*10000） 

#define LED1_PIN 					FL_GPIO_PIN_2
#define LDE1_PORT 	  				GPIOC

#define KEY1_PIN					FL_GPIO_PIN_15
#define KEY1_PORT					GPIOA

#define COUNT1_PORT					GPIOA
#define COUNT1_PIN    				FL_GPIO_PIN_10
#define COUNT1_WAKEUP_PIN 			FL_PMU_WKUP1PIN

#define COUNT2_PORT					GPIOD
#define COUNT2_PIN    				FL_GPIO_PIN_11
#define COUNT2_WAKEUP_PIN 			FL_PMU_WKUP6PIN

#define ON_LIMIT_PORT				GPIOD
#define ON_LIMIT_PIN				FL_GPIO_PIN_0
#define OFF_LIMIT_PORT 				GPIOD
#define OFF_LIMIT_PIN   			FL_GPIO_PIN_1	

#define MOTOR_A_PORT				GPIOC
#define MOTOR_A_PIN   				FL_GPIO_PIN_8
#define MOTOR_B_PORT  				GPIOC
#define MOTOR_B_PIN   				FL_GPIO_PIN_9  



#define LOG_UART					UART0
#define LOG_UART_IRQ				UART0_IRQn
#define LOG_UART_IRQ_HANDLER 		UART0_IRQHandler
#define LOG_UART_PORT				GPIOA
#define LOG_UART_TX_PIN 			FL_GPIO_PIN_14
#define LOG_UART_RX_PIN 			FL_GPIO_PIN_13


#define NB_WAKEUP_PORT				GPIOA
#define NB_WAKEUP_PIN				FL_GPIO_PIN_8

#define NB_RESET_PORT				GPIOC
#define NB_RESET_PIN				FL_GPIO_PIN_3

#define NB_POWER_PORT				GPIOA
#define NB_POWER_PIN				FL_GPIO_PIN_9

#define NB_UART						UART5
#define NB_UART_IRQ					UART5_IRQn
#define NB_UART_IRQ_HANDLER 		UART5_IRQHandler
#define NB_UART_PORT				GPIOC
#define NB_UART_TX_PIN 				FL_GPIO_PIN_5
#define NB_UART_RX_PIN 				FL_GPIO_PIN_4

#define RS485_UART					UART4
#define RS485_UART_IRQ				UART4_IRQn
#define RS485_UART_IRQ_HANDLER 		UART4_IRQHandler
#define RS485_UART_PORT				GPIOB
#define RS485_UART_TX_PIN 			FL_GPIO_PIN_3
#define RS485_UART_RX_PIN 			FL_GPIO_PIN_2

#define RS485_EN_PORT				GPIOA
#define RS485_EN_PIN 				FL_GPIO_PIN_8


#define BAT_VOTAGE_PORT				GPIOC
#define BAT_VOTAGE_PIN				FL_GPIO_PIN_10
#define BAT_VOTAGE_ADC				ADC
#define BAT_VOTAGE_CHANNEL			FL_ADC_EXTERNAL_CH1

/*****************LORA_METER_FM_V1_0****************/
#define LORA_POWER_PORT				GPIOA
#define LORA_POWER_PIN				FL_GPIO_PIN_9

#define LORA_RESET_PORT				GPIOC
#define LORA_RESET_PIN				FL_GPIO_PIN_3

#define LORA_UART					UART5
#define LORA_UART_IRQ				UART5_IRQn
#define LORA_UART_IRQ_HANDLER 		UART5_IRQHandler
#define LORA_UART_PORT				GPIOC
#define LORA_UART_TX_PIN 			FL_GPIO_PIN_5
#define LORA_UART_RX_PIN 			FL_GPIO_PIN_4
/*唤醒lora模块*/
#define WAKEUP_LORA_PORT			GPIOA
#define WAKEUP_LORA_PIN				FL_GPIO_PIN_8
/*lora模块唤醒mcu*/
#define LORA_WAKEUP_PORT			GPIOB
#define LORA_WAKEUP_PIN				FL_GPIO_PIN_2

/***************************************************/


void SelRCHFToPLL(uint32_t rchf, uint32_t clock);

void led_init(void);
void led_on(void);
void led_off(void);
void led_toggle(void);

void key_init(void);
uint8_t key_get_status(void);

void rtc_init(void);
void rtc_set_alarm(uint8_t hour, uint8_t min, uint8_t sec);
uint8_t rtc_set_time(FL_RTC_InitTypeDef *InitStructer);
uint8_t rtc_get_time(FL_RTC_InitTypeDef *InitStructer);

void lptimer32_disable(void);
void lptimer32_init(uint32_t s);

void bstimer32_init(void);


void delay_init(void);
void delay_us(uint32_t us);
void delay_ms(uint32_t ms);

void valve_init(void);
void valve_open(void);
void valve_close(void);

void rs485_uart_init(uint32_t baud);
void rs485_uart_send(uint8_t* data, uint32_t len);

void pmu_init(void);
void wakeup_pin_init(void);
void enter_low_power_mode(void);
uint8_t auto_enter_low_power_mode(void);
uint8_t check_wakeup_source(void);


void meter_cnt_init(void);
uint8_t meter_get_cnt_status(void);

void log_uart_init(uint32_t baud);
void log_uart_send(uint8_t* data, uint32_t len);
	
void iwdg_init(void);
void iwdg_feed(void);

void bat_check_init(void);
uint32_t bat_get_votage(void);

void bkp_reg_write(uint32_t reg,uint32_t value);
uint32_t bkp_reg_read(uint32_t reg);

void soft_reboot(void);

void nb_reset_enable(void);
void nb_reset_disable(void);
void nb_power_enable(void);
void nb_power_disable(void);
void nb_gpio_init(void);
void nb_uart_init(uint32_t baud);
void nb_serial_send_data(uint8_t* data, uint32_t len);


uint32_t flash_read(uint32_t addr, uint8_t* data, uint32_t len);
uint32_t flash_write(uint32_t addr, uint8_t* data, uint32_t len);

void lora_reset_enable(void);
void lora_reset_disable(void);
/*开nb电源*/
void lora_power_enable(void);
/*关闭nb电源*/
void lora_power_disable(void);
void lora_wakeup_disable(void);
void lora_wakeup_enable(void);
void lora_gpio_init(void);
void lora_uart_init(uint32_t baud);
void lora_serial_send_data(uint8_t* data, uint32_t len);
#endif

